var pgpromise = require( 'pg-promise' )()

const pgConfig = {
    user: 'postgres',
    password: '123456',
    database: 'postgres',                                                                                   
    host: '127.0.0.1',
    port: '5432',
    poolSize: 5,
}   


async function main () { 
    var db = pgpromise( pgConfig );
    var dbindex = await db.query( "SELECT shops.* , protectors.mac FROM shops, protectors WHERE shops.id = protectors.shopid " );
    console.log( "_--------------" );
    console.log( dbindex );

    //var ret = await  db.query( "INSERT INTO protector_alarm_log( shopid, mac, stamp, arm_area_idx )  VALUES( $1, $2, $3, $4 )",[ "wm01",  "11:22:33:44:55:78", Date.now(), "1" ] );
    var ret = await  db.query( "INSERT INTO protector_alarm_log( shopid, mac, stamp, arm_area_idx )  SELECT shopid, $1,  $2, $3 FROM protectors WHERE mac= CAST( $1 AS macaddr) ", [  "11:22:33:44:55:66", Date.now(), "1" ] );
    ret = await db.query( "SELECT * from protector_alarm_log " )
    console.log( ret );
    var shopid = await db.one( "SELECT shopid from  protectors WHERE mac = $1" , [ "112233445566" ] );
    console.log( "shopid: ", shopid );
    
}

main();
        

